package com.cms.common.util;

import java.io.File;
import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.cms.model.Mail;
import com.cms.model.MailRecord;

public class MailHelper {
	
	private final static Logger log=Logger.getLogger(MailHelper.class);
	
	
	private final  static int SYSTEM_MAIL=0;
	private final  static int USER_MAIL=1;
	
	private String host;
	
	private String account;//账号
	
	private String password;
	
	private String nickname;//显示名称
	
	private String email;//显示发送邮件地址
	
	private static MailHelper instance;
	
	private MailHelper(){
		init();
	}
	
	/**
	 * 初始化发送邮件账号信息
	 */
	private void init() {
		Mail mailConfig = Mail.dao.findById(1);
		if(mailConfig==null){
			throw new IllegalArgumentException("没有配置发送邮件参数！");
		}
		host=mailConfig.getStr("smtp_host");
		account=mailConfig.getStr("smtp_user");
		password=mailConfig.getStr("smtp_pass");
		nickname=mailConfig.getStr("smtp_name");
		email=mailConfig.getStr("smtp_email");
	}

	public static MailHelper getInstance(){
		if(instance==null){
			instance=new MailHelper();
		}
		return instance;
	}
	
	
	public boolean sendMail(String toMail,String subject,String content,int type){
		return sendMail(toMail, subject, content, null, type);
	}
	
	public boolean sendSystemMail(String toMail,String subject,String content){
		return sendMail(toMail, subject, content, MailHelper.SYSTEM_MAIL);
	}
	
	public boolean sendUserMail(String toMail,String subject,String content){
		return sendMail(toMail, subject, content, MailHelper.USER_MAIL);
	}
	
	public boolean sendMail(String toMail,String subject,String content,List<File> attachments,int type){
		JavaMailSenderImpl jms=new JavaMailSenderImpl();
		MimeMessage mailMessage = jms.createMimeMessage(); 
		try {
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
			 //设置收件人，寄件人 
		    messageHelper.setTo(toMail);    
		    messageHelper.setFrom(this.email); 
		    messageHelper.setSubject(subject);
		    jms.setHost(this.host);
		    jms.setUsername(this.account);
		    jms.setPassword(this.password);
		    //注意图片的地址，外网可以访问的地址
		    messageHelper.setText(content,true);
		    if(attachments!=null&&attachments.size()>0){
			   // FileSystemResource file = new FileSystemResource(new File("g:/test.rar")); 
			   // messageHelper.addAttachment("test.rar",file);
		    }
		    Properties prop = new Properties() ;
		    prop.put("mail.smtp.auth", "true") ; // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
		    prop.put("mail.smtp.timeout", "25000") ; 
		    jms.setJavaMailProperties(prop); 
		    jms.send(mailMessage);
		} catch (MessagingException e) {
			e.printStackTrace();
			log.error("发送给"+toMail+"的邮件失败！", e);
			MailRecord.dao.addRecord(toMail,subject,content,attachments,false,this.nickname,type,this.email);
			return false;
		}
		MailRecord.dao.addRecord(toMail,subject,content,attachments,true,this.nickname,type,this.email);
		return true;
	}
	
}
